import { _decorator, Component, instantiate, Label, Node, tween, UITransform, Vec3 } from 'cc';
import { Global } from './Global';
import { Clips } from './Enums';
import { AudioManger } from './AudioManger';
import { BasePage } from './BasePage';
import { AdManger } from './ad/AdManger';
import { Tools } from './Tools';
import { ResourcePool } from './ResourcePool';

const { ccclass, property } = _decorator;

@ccclass('SuccessAction')
export class SuccessAction extends BasePage {

    @property(Label)
    level_label: Label = null!;

    @property(Label)
    coins_label: Label = null!;

    @property(Node)
    coins_img: Node = null!;

    @property(Node)
    get_coins_btn: Node = null!;

    @property(Node)
    next_btn: Node = null!;

    start() {
        super.start();
    }

    update(deltaTime: number) {
        // 暂时无用，若不需要可删除
    }

    open() {
        super.open();
        this.get_coins_btn && (this.get_coins_btn.active = true);

        AudioManger.instance.playSound(Clips.complete_2);

        this.level_label.string = Global.cur_lvl.toString();

        this.coins_label.string = Global.cur_coins.toString() + " ";
        this.coins_label.node.scale = new Vec3(3, 3, 1);  // Z轴改为1更合理，避免缩放异常

        tween(this.coins_label.node)
            .to(0.5, { scale: new Vec3(1, 1, 1) }, Global.our_easing)
            .start();

        if (this.next_btn) {
            this.next_btn.active = false;
            this.scheduleOnce(() => {
                this.next_btn.active = true;
            }, 1);
        }
    }

    btn_next() {
        AudioManger.instance.playSound(Clips.btn_1);
        if (this.get_coins_btn) {
            this.get_coins_btn.active = false;
        }
        this.close();
        AdManger.show_interstial();

        // 重置关卡或跳转下一关
        // Global.reset_level_default();
        // Global.main_action.next_lvl();
        Global.main_action.startLevel(Global.next_level());
    }

    private play_add_gift_tween(num: number, world_pos: Vec3, callBack: () => void) {
        const prefab = ResourcePool.instance.get_prefab("coin");
        let completed = 0;

        for (let i = 0; i < num; i++) {
            const coin = instantiate(prefab);
            const localPos = this.coins_img.getComponent(UITransform).convertToNodeSpaceAR(world_pos);
            this.coins_img.addChild(coin);

            // 位置加上随机偏移
            localPos.x += Tools.random_between(-30, 80);
            localPos.y += Tools.random_between(0, 150);
            coin.setPosition(localPos);

            tween(coin)
                .to(Tools.random_between(0.5, 1.2), { position: Vec3.ZERO }, Global.our_easing)
                .call(() => {
                    completed++;
                    this.coins_label.string = (Global.cur_coins + completed).toString();
                    AudioManger.instance.playSound(Clips.coins);

                    if (completed >= num) {
                        callBack && callBack();
                        // 结束后恢复显示当前金币数
                        this.coins_label.string = Global.cur_coins.toString();
                    }
                })
                .removeSelf()
                .start();
        }
    }

    get_one_bag_coins() {
        if (!this.get_coins_btn) return;
        this.get_coins_btn.active = false;

        AudioManger.instance.playSound(Clips.btn_1);

        AdManger.show_video((data) => {
            if (data === 1) {
                console.log("get_one_bag_coins 获取奖励成功");
                const num = Tools.random_between_floor(15, 46);

                this.play_add_gift_tween(num, this.get_coins_btn.getWorldPosition(), () => {
                    Global.coins_action.put_coins(num, this.coins_img.getWorldPosition(), false);
                });
            } else {
                Global.tips_action.show("获取奖励失败");
                // 可以根据需求决定是否关闭界面或其他操作
            }
        });
    }
}
